/*
 * CancelaListaAction.java
 *
 * Created on August 6, 2007, 11:47 PM
 */

package br.usp.poli.mfc.struts.action;

import br.usp.poli.mfc.struts.DAO.DAOFactory;
import br.usp.poli.mfc.struts.DAO.ListaDAO;
import br.usp.poli.mfc.struts.DAO.RevistaDAO;
import br.usp.poli.mfc.struts.bean.Lista;
import br.usp.poli.mfc.struts.bean.Revista;
import br.usp.poli.mfc.struts.bean.Usuario;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
/**
 *
 * @author William T.
 * @version
 */

public class CancelaListaAction extends Action {
    
    private final static String SUCCESS = "success";
    
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        HttpSession session = request.getSession();
        Usuario user = (Usuario)session.getAttribute("usuario");
        RevistaDAO revistaDAO = DAOFactory.getRevistaDAO();
        ListaDAO listaDAO = DAOFactory.getListaDAO();
        ArrayList<Lista> inscritas = listaDAO.getListaByUsuario(user.getId_usuario());
        ArrayList<Revista> revInscritas = new ArrayList<Revista>();
        if(inscritas != null) {
            for (int i = 0; i < inscritas.size(); i++) {
                Revista rev = revistaDAO.getRevistaByID(inscritas.get(i).getIDRevista(),true); // essa boolean eh POG
                revInscritas.add(rev);
            }
        }
        if(session.getAttribute("cancelaRevistaLista") != null) session.removeAttribute("cancelaRevistaLista");
        session.setAttribute("cancelaRevistaLista",revInscritas);
        return mapping.findForward(SUCCESS);
    }
}
